package com.huida.app.equal.compareto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author wangzhuangzhuang
 * @DESC:
 * @date 2022-01-02 20:30
 */
@Slf4j
public class CompareToController {

	public static void main(String[] args) {
		CompareToController ctc = new CompareToController();
//		ctc.wrong();
		ctc.right();
	}

	public void wrong(){
		ArrayList<Student> list = new ArrayList<>();
		list.add(new Student(1, "wang"));
		list.add(new Student(2, "zz"));
		Student stu = new Student(3, "li");
		log.info("ArrayList.indexOf");
		int index01 = list.indexOf(stu);
		Collections.sort(list);
		log.info("Collections.binarySearch");
		int index02 = Collections.binarySearch(list, stu);
		log.info("index01 = " + index01);
		log.info("index02 = " + index02);
	}

	public void right(){
		List<StudentRight> list = new ArrayList<StudentRight>();
		list.add(new StudentRight(1, "wang"));
		list.add(new StudentRight(2, "zz"));
		StudentRight stuR = new StudentRight(2, "li");
		log.info("ArrayList.indexOf");
		int index01 = list.indexOf(stuR);
		Collections.sort(list);
		log.info("Collections.binarySearch");
		int index02 = Collections.binarySearch(list, stuR);
		log.info("index01 = " + index01);
		log.info("index02 = " + index02);
	}

	@Data
	@AllArgsConstructor
	class Student implements Comparable<Student>{
		private Integer id;
		private String name;
		@Override
		public int compareTo(Student other) {
			int result = Integer.compare(other.id, id);
			if(result == 0){
				log.info("this {} == other {}", this, other);
			}
			return 0;
		}
	}

	@Data
	@AllArgsConstructor
	class StudentRight implements Comparable<StudentRight>{
		private Integer id;
		private String name;

		@Override
		public int compareTo(StudentRight other){
			return Comparator.comparing(StudentRight::getName)
					.thenComparing(StudentRight::getId)
					.compare(this, other);
		}
	}
}

